/*
 * @Author: szx
 * @Date: 2021-12-21 17:12:57
 * @LastEditTime: 2021-12-21 17:30:56
 * @Description:
 * @FilePath: \leetcode\200-299\257\257.js
 */
/**
 * @param {TreeNode} root
 * @return {string[]}
 */
var binaryTreePaths = function (root) {
    const que = [root];
    const paths = [root.val.toString()];
    const res = [];
    while (que.length) {
        let node = que.shift();
        let path = paths.shift();
        if (node.left === null && node.right === null) {
            res.push(path);
        } else {
            if (node.left) {
                que.push(node.left);
                paths.push(path + '->' + node.left.val.toString());
            }
            if (node.right) {
                que.push(node.right);
                paths.push(path + '->' + node.right.val.toString());
            }
        }
    }
    return res;
};

var binaryTreePaths = function (root) {
    const res = [];
    const getPath = (node, curPath) => {
        if (!node.left && !node.right) {
            curPath += node.val;
            res.push(curPath);
            return;
        }
        curPath += node.val + '->';
        node.left && getPath(node.left, curPath);
        node.right && getPath(node.right, curPath);
    };
    getPath(root, '');
    return res;
};
